home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / ZSI / TCcompound.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2008-10-13  |  17.4 KB  |  537 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from ZSI import _copyright, _children, _child_elements, _inttypes, _stringtypes, _seqtypes, _find_arraytype, _find_href, _find_type, _find_xmlns_prefix, _get_idstr, EvaluateException, ParseException
  5. from TC import _get_element_nsuri_name, _get_xsitype, TypeCode, Any, AnyElement, AnyType, Nilled, UNBOUNDED
  6. from schema import GED, ElementDeclaration, TypeDefinition, _get_substitute_element, _get_type_definition, _is_substitute_element
  7. from ZSI.wstools.Namespaces import SCHEMA, SOAP
  8. from ZSI.wstools.Utility import SplitQName
  9. from ZSI.wstools.logging import getLogger as _GetLogger
  10. import re
  11. import types
  12. from copy import copy as _copy
  13.  
  14. _find_arrayoffset = lambda E: E.getAttributeNS(SOAP.ENC, 'offset')
  15.  
  16. _find_arrayposition = lambda E: E.getAttributeNS(SOAP.ENC, 'position')
  17. _offset_pat = re.compile('\\[[0-9]+\\]')
  18. _position_pat = _offset_pat
  19.  
  20. def _check_typecode_list(ofwhat, tcname):
  21.     for o in ofwhat:
  22.         if callable(o):
  23.             continue
  24.         
  25.         if not isinstance(o, TypeCode):
  26.             raise TypeError(tcname + ' ofwhat outside the TypeCode hierarchy, ' + str(o.__class__))
  27.         
  28.         if o.pname is None and not isinstance(o, AnyElement):
  29.             raise TypeError(tcname + ' element ' + str(o) + ' has no name')
  30.             continue
  31.     
  32.  
  33.  
  34. def _get_type_or_substitute(typecode, pyobj, sw, elt):
  35.     sub = getattr(pyobj, 'typecode', typecode)
  36.     if sub is typecode or sub is None:
  37.         return typecode
  38.     
  39.     if isinstance(typecode, AnyElement):
  40.         return sub
  41.     
  42.     if isinstance(sub, ElementDeclaration):
  43.         if (typecode.nspname, typecode.pname) == (sub.nspname, sub.pname):
  44.             raise TypeError('bad usage, failed to serialize element reference (%s, %s), in: %s' % (typecode.nspname, typecode.pname, sw.Backtrace(elt)))
  45.         
  46.         if _is_substitute_element(typecode, sub):
  47.             return sub
  48.         
  49.         raise TypeError('failed to serialize (%s, %s) illegal sub GED (%s,%s): %s' % (typecode.nspname, typecode.pname, sub.nspname, sub.pname, sw.Backtrace(elt)))
  50.     
  51.     if not isinstance(typecode, AnyType) and not isinstance(sub, typecode.__class__):
  52.         raise TypeError('failed to serialize substitute %s for %s,  not derivation: %s' % (sub, typecode, sw.Backtrace(elt)))
  53.     
  54.     sub = _copy(sub)
  55.     sub.nspname = typecode.nspname
  56.     sub.pname = typecode.pname
  57.     sub.aname = typecode.aname
  58.     sub.minOccurs = sub.maxOccurs = 1
  59.     return sub
  60.  
  61.  
  62. class ComplexType(TypeCode):
  63.     logger = _GetLogger('ZSI.TCcompound.ComplexType')
  64.     
  65.     def __init__(self, pyclass, ofwhat, pname = None, inorder = False, inline = False, mutable = True, mixed = False, mixed_aname = '_text', **kw):
  66.         TypeCode.__init__(self, pname, pyclass = pyclass, **kw)
  67.         self.inorder = inorder
  68.         self.inline = inline
  69.         self.mutable = mutable
  70.         self.mixed = mixed
  71.         self.mixed_aname = None
  72.         if mixed is True:
  73.             self.mixed_aname = mixed_aname
  74.         
  75.         if self.mutable is True:
  76.             self.inline = True
  77.         
  78.         if not kw.get('type'):
  79.             pass
  80.         self.type = _get_xsitype(self)
  81.         t = type(ofwhat)
  82.         if t not in _seqtypes:
  83.             raise TypeError('Struct ofwhat must be list or sequence, not ' + str(t))
  84.         
  85.         self.ofwhat = tuple(ofwhat)
  86.         if TypeCode.typechecks:
  87.             if self.pyclass is not None and type(self.pyclass) is not types.ClassType and not isinstance(self.pyclass, object):
  88.                 raise TypeError('pyclass must be None or an old-style/new-style class, not ' + str(type(self.pyclass)))
  89.             
  90.             _check_typecode_list(self.ofwhat, 'ComplexType')
  91.         
  92.  
  93.     
  94.     def parse(self, elt, ps):
  95.         debug = self.logger.debugOn()
  96.         if debug:
  97.             pass
  98.         self.logger.debug('parse')
  99.         xtype = self.checkname(elt, ps)
  100.         if self.type and xtype not in [
  101.             self.type,
  102.             (None, None)]:
  103.             if not isinstance(self, TypeDefinition):
  104.                 raise EvaluateException('ComplexType for %s has wrong type(%s), looking for %s' % (self.pname, self.checktype(elt, ps), self.type), ps.Backtrace(elt))
  105.             elif debug:
  106.                 pass
  107.             self.logger.debug('delegate to substitute type')
  108.             what = TypeDefinition.getSubstituteType(self, elt, ps)
  109.             return what.parse(elt, ps)
  110.         
  111.         href = _find_href(elt)
  112.         if href:
  113.             if _children(elt):
  114.                 raise EvaluateException('Struct has content and HREF', ps.Backtrace(elt))
  115.             
  116.             elt = ps.FindLocalHREF(href, elt)
  117.         
  118.         c = _child_elements(elt)
  119.         count = len(c)
  120.         if self.nilled(elt, ps):
  121.             return Nilled
  122.         
  123.         v = { }
  124.         attributes = self.parse_attributes(elt, ps)
  125.         if attributes:
  126.             v[self.attrs_aname] = attributes
  127.         
  128.         if self.mixed is True:
  129.             v[self.mixed_aname] = self.simple_value(elt, ps, mixed = True)
  130.         
  131.         c = c[:]
  132.         crange = range(len(c))
  133.         if debug:
  134.             self.logger.debug('ofwhat: %s', str(self.ofwhat))
  135.         
  136.         any = None
  137.         for i in range(len(self.ofwhat)):
  138.             pass
  139.         
  140.         if any is not None:
  141.             occurs = 0
  142.             v[any.aname] = []
  143.             for j in crange:
  144.                 if c[j]:
  145.                     continue
  146.                 _[3][(j, c[j])]
  147.             
  148.             if any.maxOccurs == 1 and occurs == 0:
  149.                 v[any.aname] = None
  150.             elif (occurs < any.minOccurs or any.maxOccurs != UNBOUNDED) and any.maxOccurs < occurs:
  151.                 raise EvaluateException('occurances of <any> elements(#%d) bound by (%d,%s)' % (occurs, any.minOccurs, str(any.maxOccurs)), ps.Backtrace(elt))
  152.             
  153.         
  154.         if not self.pyclass:
  155.             return v
  156.         
  157.         
  158.         try:
  159.             pyobj = self.pyclass()
  160.         except Exception:
  161.             e = None
  162.             raise TypeError('Constructing element (%s,%s) with pyclass(%s), %s' % (self.nspname, self.pname, self.pyclass.__name__, str(e)))
  163.  
  164.         for key in v.keys():
  165.             setattr(pyobj, key, v[key])
  166.         
  167.         return pyobj
  168.  
  169.     
  170.     def serialize(self, elt, sw, pyobj, inline = False, name = None, **kw):
  171.         if inline or self.inline:
  172.             self.cb(elt, sw, pyobj, name = name, **kw)
  173.         else:
  174.             objid = _get_idstr(pyobj)
  175.             (ns, n) = self.get_name(name, objid)
  176.             el = elt.createAppendElement(ns, n)
  177.             el.setAttributeNS(None, 'href', '#%s' % objid)
  178.             sw.AddCallback(self.cb, elt, sw, pyobj)
  179.  
  180.     
  181.     def cb(self, elt, sw, pyobj, name = None, **kw):
  182.         debug = self.logger.debugOn()
  183.         if debug:
  184.             self.logger.debug('cb: %s' % str(self.ofwhat))
  185.         
  186.         objid = _get_idstr(pyobj)
  187.         (ns, n) = self.get_name(name, objid)
  188.         if pyobj is None:
  189.             if self.nillable is True:
  190.                 elem = elt.createAppendElement(ns, n)
  191.                 self.serialize_as_nil(elem)
  192.                 return None
  193.             
  194.             raise EvaluateException, 'element(%s,%s) is not nillable(%s)' % (self.nspname, self.pname, self.nillable)
  195.         
  196.         if self.mutable is False and sw.Known(pyobj):
  197.             return None
  198.         
  199.         if debug:
  200.             self.logger.debug('element: (%s, %s)', str(ns), n)
  201.         
  202.         if n is not None:
  203.             elem = elt.createAppendElement(ns, n)
  204.             self.set_attributes(elem, pyobj)
  205.             if kw.get('typed', self.typed) is True:
  206.                 self.set_attribute_xsi_type(elem)
  207.             
  208.             if self.mixed is True and self.mixed_aname is not None:
  209.                 if hasattr(pyobj, self.mixed_aname):
  210.                     textContent = getattr(pyobj, self.mixed_aname)
  211.                     if hasattr(textContent, 'typecode'):
  212.                         textContent.typecode.serialize_text_node(elem, sw, textContent)
  213.                     elif type(textContent) in _stringtypes:
  214.                         if debug:
  215.                             self.logger.debug('mixed text content:\n\t%s', textContent)
  216.                         
  217.                         elem.createAppendTextNode(textContent)
  218.                     else:
  219.                         raise EvaluateException('mixed test content in element (%s,%s) must be a string type' % (self.nspname, self.pname), sw.Backtrace(elt))
  220.                 elif debug:
  221.                     self.logger.debug('mixed NO text content in %s', self.mixed_aname)
  222.                 
  223.             
  224.         else:
  225.             elem = elt
  226.         if self.inline:
  227.             pass
  228.         elif not (self.inline) and self.unique:
  229.             raise EvaluateException('Not inline, but unique makes no sense. No href/id.', sw.Backtrace(elt))
  230.         elif n is not None:
  231.             self.set_attribute_id(elem, objid)
  232.         
  233.         if self.pyclass and type(self.pyclass) is type:
  234.             
  235.             f = lambda attr: getattr(pyobj, attr, None)
  236.         elif self.pyclass:
  237.             d = pyobj.__dict__
  238.             
  239.             f = lambda attr: d.get(attr)
  240.         else:
  241.             d = pyobj
  242.             
  243.             f = lambda attr: pyobj.get(attr)
  244.             if TypeCode.typechecks and type(d) != types.DictType:
  245.                 raise TypeError("Classless complexType didn't get dictionary")
  246.             
  247.         indx = 0
  248.         lenofwhat = len(self.ofwhat)
  249.         if debug:
  250.             self.logger.debug('element declaration (%s,%s)', self.nspname, self.pname)
  251.             if self.type:
  252.                 self.logger.debug('xsi:type definition (%s,%s)', self.type[0], self.type[1])
  253.             else:
  254.                 self.logger.warning('NO xsi:type')
  255.         
  256.         while indx < lenofwhat:
  257.             occurs = 0
  258.             what = self.ofwhat[indx]
  259.             if callable(what):
  260.                 what = what()
  261.             
  262.             if debug:
  263.                 self.logger.debug('serialize what -- %s', what.__class__.__name__)
  264.             
  265.             aname = what.aname
  266.             v = f(aname)
  267.             indx += 1
  268.             if what.minOccurs == 0 and v is None:
  269.                 continue
  270.             
  271.             whatTC = what
  272.             if whatTC.maxOccurs > 1 and v is not None:
  273.                 if type(v) not in _seqtypes:
  274.                     raise EvaluateException('pyobj (%s,%s), aname "%s": maxOccurs %s, expecting a %s' % (self.nspname, self.pname, what.aname, whatTC.maxOccurs, _seqtypes), sw.Backtrace(elt))
  275.                 
  276.                 for v2 in v:
  277.                     occurs += 1
  278.                     if occurs > whatTC.maxOccurs:
  279.                         raise EvaluateException('occurances (%d) exceeded maxOccurs(%d) for <%s>' % (occurs, whatTC.maxOccurs, what.pname), sw.Backtrace(elt))
  280.                     
  281.                     what = _get_type_or_substitute(whatTC, v2, sw, elt)
  282.                     if debug and what is not whatTC:
  283.                         self.logger.debug('substitute derived type: %s' % what.__class__)
  284.                     
  285.                     what.serialize(elem, sw, v2, **kw)
  286.                 
  287.                 if occurs < whatTC.minOccurs:
  288.                     raise EvaluateException('occurances(%d) less than minOccurs(%d) for <%s>' % (occurs, whatTC.minOccurs, what.pname), sw.Backtrace(elt))
  289.                     continue
  290.                 continue
  291.             
  292.             if v is not None or what.nillable is True:
  293.                 what = _get_type_or_substitute(whatTC, v, sw, elt)
  294.                 if debug and what is not whatTC:
  295.                     self.logger.debug('substitute derived type: %s' % what.__class__)
  296.                 
  297.                 what.serialize(elem, sw, v, **kw)
  298.                 continue
  299.             
  300.             raise EvaluateException('Got None for nillable(%s), minOccurs(%d) element (%s,%s), %s' % (what.nillable, what.minOccurs, what.nspname, what.pname, elem), sw.Backtrace(elt))
  301.  
  302.     
  303.     def setDerivedTypeContents(self, extensions = None, restrictions = None):
  304.         if extensions:
  305.             ofwhat = list(self.ofwhat)
  306.             if type(extensions) in _seqtypes:
  307.                 ofwhat += list(extensions)
  308.             else:
  309.                 ofwhat.append(extensions)
  310.         elif restrictions:
  311.             if type(restrictions) in _seqtypes:
  312.                 ofwhat = restrictions
  313.             else:
  314.                 ofwhat = (restrictions,)
  315.         else:
  316.             return None
  317.         self.ofwhat = tuple(ofwhat)
  318.         self.lenofwhat = len(self.ofwhat)
  319.  
  320.  
  321.  
  322. class Struct(ComplexType):
  323.     logger = _GetLogger('ZSI.TCcompound.Struct')
  324.     
  325.     def __init__(self, pyclass, ofwhat, pname = None, inorder = False, inline = False, mutable = True, **kw):
  326.         ComplexType.__init__(self, pyclass, ofwhat, pname = pname, inorder = inorder, inline = inline, mutable = mutable, **kw)
  327.         whats = map((lambda what: (what.nspname, what.pname)), self.ofwhat)
  328.         for idx in range(len(self.ofwhat)):
  329.             what = self.ofwhat[idx]
  330.             key = (what.nspname, what.pname)
  331.             if not isinstance(what, AnyElement) and what.maxOccurs > 1:
  332.                 raise TypeError, 'Constraint: no element can have a maxOccurs>1'
  333.             
  334.             if key in whats[idx + 1:]:
  335.                 raise TypeError, 'Constraint: No element may have the same name as any other'
  336.                 continue
  337.         
  338.  
  339.  
  340.  
  341. class Array(TypeCode):
  342.     logger = _GetLogger('ZSI.TCcompound.Array')
  343.     
  344.     def __init__(self, atype, ofwhat, pname = None, dimensions = 1, fill = None, sparse = False, mutable = False, size = None, nooffset = 0, undeclared = False, childnames = None, **kw):
  345.         TypeCode.__init__(self, pname, **kw)
  346.         self.dimensions = dimensions
  347.         self.atype = atype
  348.         if undeclared is False and self.atype[1].endswith(']') is False:
  349.             self.atype = (self.atype[0], '%s[]' % self.atype[1])
  350.         
  351.         if self.dimensions != 1:
  352.             raise TypeError('Only single-dimensioned arrays supported')
  353.         
  354.         self.fill = fill
  355.         self.sparse = sparse
  356.         self.mutable = mutable
  357.         self.size = size
  358.         self.nooffset = nooffset
  359.         self.undeclared = undeclared
  360.         self.childnames = childnames
  361.         if self.size:
  362.             t = type(self.size)
  363.             if t in _inttypes:
  364.                 self.size = (self.size,)
  365.             elif t in _seqtypes:
  366.                 self.size = tuple(self.size)
  367.             elif TypeCode.typechecks:
  368.                 raise TypeError('Size must be integer or list, not ' + str(t))
  369.             
  370.         
  371.         if not ofwhat:
  372.             pass
  373.         ofwhat = Any()
  374.         if TypeCode.typechecks:
  375.             if self.undeclared is False and type(atype) not in _seqtypes and len(atype) == 2:
  376.                 raise TypeError('Array type must be a sequence of len 2.')
  377.             
  378.             t = type(ofwhat)
  379.             if not isinstance(ofwhat, TypeCode):
  380.                 raise TypeError('Array ofwhat outside the TypeCode hierarchy, ' + str(ofwhat.__class__))
  381.             
  382.             if self.size:
  383.                 if len(self.size) != self.dimensions:
  384.                     raise TypeError('Array dimension/size mismatch')
  385.                 
  386.                 for s in self.size:
  387.                     if type(s) not in _inttypes:
  388.                         raise TypeError('Array size "' + str(s) + '" is not an integer.')
  389.                         continue
  390.                 
  391.             
  392.         
  393.         self.ofwhat = ofwhat
  394.  
  395.     
  396.     def parse_offset(self, elt, ps):
  397.         o = _find_arrayoffset(elt)
  398.         if not o:
  399.             return 0
  400.         
  401.         if not _offset_pat.match(o):
  402.             raise EvaluateException('Bad offset "' + o + '"', ps.Backtrace(elt))
  403.         
  404.         return int(o[1:-1])
  405.  
  406.     
  407.     def parse_position(self, elt, ps):
  408.         o = _find_arrayposition(elt)
  409.         if not o:
  410.             return None
  411.         
  412.         if o.find(',') > -1:
  413.             raise EvaluateException('Sorry, no multi-dimensional arrays', ps.Backtrace(elt))
  414.         
  415.         if not _position_pat.match(o):
  416.             raise EvaluateException('Bad array position "' + o + '"', ps.Backtrace(elt))
  417.         
  418.         return int(o[1:-1])
  419.  
  420.     
  421.     def parse(self, elt, ps):
  422.         href = _find_href(elt)
  423.         if href:
  424.             if _children(elt):
  425.                 raise EvaluateException('Array has content and HREF', ps.Backtrace(elt))
  426.             
  427.             elt = ps.FindLocalHREF(href, elt)
  428.         
  429.         if self.nilled(elt, ps):
  430.             return Nilled
  431.         
  432.         if not _find_arraytype(elt) and self.undeclared is False:
  433.             raise EvaluateException('Array expected', ps.Backtrace(elt))
  434.         
  435.         t = _find_type(elt)
  436.         if t:
  437.             pass
  438.         
  439.         offset = self.parse_offset(elt, ps)
  440.         v = []
  441.         vlen = 0
  442.         if offset and not (self.sparse):
  443.             while vlen < offset:
  444.                 vlen += 1
  445.                 v.append(self.fill)
  446.         
  447.         for c in _child_elements(elt):
  448.             item = self.ofwhat.parse(c, ps)
  449.             if not self.parse_position(c, ps):
  450.                 pass
  451.             position = offset
  452.             if self.sparse:
  453.                 v.append((position, item))
  454.             else:
  455.                 while offset < position:
  456.                     offset += 1
  457.                     v.append(self.fill)
  458.                 v.append(item)
  459.             offset += 1
  460.         
  461.         return v
  462.  
  463.     
  464.     def serialize(self, elt, sw, pyobj, name = None, childnames = None, **kw):
  465.         debug = self.logger.debugOn()
  466.         if debug:
  467.             self.logger.debug('serialize: %r' % pyobj)
  468.         
  469.         if self.mutable is False and sw.Known(pyobj):
  470.             return None
  471.         
  472.         objid = _get_idstr(pyobj)
  473.         (ns, n) = self.get_name(name, objid)
  474.         el = elt.createAppendElement(ns, n)
  475.         if self.nillable is True and pyobj is None:
  476.             self.serialize_as_nil(el)
  477.             return None
  478.         
  479.         self.set_attributes(el, pyobj)
  480.         if not self.unique:
  481.             pass
  482.         unique = kw.get('unique', False)
  483.         if unique is False and sw.Known(pyobj):
  484.             self.set_attribute_href(el, objid)
  485.             return None
  486.         
  487.         if kw.get('typed', self.typed) is True:
  488.             self.set_attribute_xsi_type(el, **kw)
  489.         
  490.         if self.unique is False:
  491.             self.set_attribute_id(el, objid)
  492.         
  493.         offset = 0
  494.         if self.sparse is False and self.nooffset is False:
  495.             offset = 0
  496.             end = len(pyobj)
  497.             while offset < end and pyobj[offset] == self.fill:
  498.                 offset += 1
  499.             if offset:
  500.                 el.setAttributeNS(SOAP.ENC, 'offset', '[%d]' % offset)
  501.             
  502.         
  503.         if self.undeclared is False:
  504.             el.setAttributeNS(SOAP.ENC, 'arrayType', '%s:%s' % (el.getPrefix(self.atype[0]), self.atype[1]))
  505.         
  506.         if debug:
  507.             self.logger.debug('ofwhat: %r' % self.ofwhat)
  508.         
  509.         d = { }
  510.         if not childnames:
  511.             pass
  512.         kn = self.childnames
  513.         if kn:
  514.             d['name'] = kn
  515.         elif not self.ofwhat.aname:
  516.             d['name'] = 'element'
  517.         
  518.         if self.sparse is False:
  519.             for e in pyobj[offset:]:
  520.                 self.ofwhat.serialize(el, sw, e, **d)
  521.             
  522.         else:
  523.             position = 0
  524.             for pos, v in pyobj:
  525.                 if pos != position:
  526.                     el.setAttributeNS(SOAP.ENC, 'position', '[%d]' % pos)
  527.                     position = pos
  528.                 
  529.                 self.ofwhat.serialize(el, sw, v, **d)
  530.                 position += 1
  531.             
  532.  
  533.  
  534. if __name__ == '__main__':
  535.     print _copyright
  536.  
  537.